package com.file.service;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.Transactional;

import org.json.JSONObject;
import org.json.JSONTokener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.entity.Collector;
import com.entity.Customer;
import com.entity.Org;
import com.file.entity.FileCollector;
import com.file.entity.FileCustomer;
import com.interfaces.CollectorDao;
import com.interfaces.MeasuringpointDao;
import com.interfaces.MeterDao;
import com.interfaces.OrgDao;
import com.interfaces.SysdicDao;
import com.interfaces.TerminalDao;
import com.setting.service.SysdicService;
import com.utils.JsonResult;
import com.utils.Utils;

/**
 * @Title: FileService.java
 * @Package com.service
 * @author 孙梦楠
 * @date 2017年7月20日 上午10:04:05
 * @version V1.0.0
 */

@Service
public class CollectorService {

	private static final Logger log = LoggerFactory.getLogger(CollectorService.class);

	@Autowired
	private SysdicDao sysdicdao; // 系统字典

	@Autowired
	private CollectorDao collectordao; // 采集器

	@Autowired
	private MeasuringpointDao measuringpointdao;

	@Autowired
	private MeterDao meterdao;

	@Autowired
	private TerminalDao terminaldao;

	@Autowired
	private SysdicService sysdicservice;

	@Autowired
	private OrgDao orgdao;

	// 列表
	@Transactional
	public JsonResult<List<FileCollector>> listcollector(String opeorgcookie, String opetypecookie,
			String languagecookie) {

		List<Collector> collectors = collectordao
				.findByCollectororgAndTypeAndCollectorstatusNot(Integer.valueOf(opeorgcookie), opetypecookie, "4");
		List<FileCollector> filecollectors = new ArrayList<FileCollector>();
		// 逐条查询
		if (collectors.size() != 0) {
			for (int i = 0; i < collectors.size(); i++) {

				Collector collector = collectors.get(i);

				filecollectors.add(dofilecollector(collector, languagecookie));
			}
		}
		return new JsonResult<List<FileCollector>>(33, "展示采集器所有列表", filecollectors);
	}

	// 条件列表
	@Transactional
	public JsonResult<List<FileCollector>> listCollectorByConditions(String collectorcode, String collectorassetcode,
			String collectortype, String collectorprotocol, String collectormade, String collectorstatus,
			String opeorgcookie, String opetypecookie, String languagecookie) {

		if (Utils.isNull(collectorcode) && Utils.isNull(collectorassetcode) && Utils.isNull(collectortype)
				&& Utils.isNull(collectorprotocol) && Utils.isNull(collectormade) && Utils.isNull(collectorstatus)) {
			List<FileCollector> filecollectors = new ArrayList<FileCollector>();
			if (opeorgcookie.equals("1")) {
				List<Collector> collectors = collectordao.findByTypeAndCollectorstatusNot(opetypecookie, "4");
				for (Collector collector : collectors) {
					filecollectors.add(dofilecollector(collector, languagecookie));
				}
			} else if (orgdao.findByOrgvalue(opeorgcookie).getOrgfather().equals("0")) {
				List<Org> orgs = orgdao.findByOrgfather(opeorgcookie);
				for (Org org : orgs) {
					List<Collector> collectors = collectordao.findByCollectororgAndTypeAndCollectorstatusNot(
							Integer.parseInt(org.getOrgvalue()), opetypecookie, "4");

					for (Collector collector : collectors) {
						filecollectors.add(dofilecollector(collector, languagecookie));
					}
				}
			} else {
				List<Collector> collectors = collectordao.findByCollectororgAndTypeAndCollectorstatusNot(
						Integer.valueOf(opeorgcookie), opetypecookie, "4");
				// 逐条查询
				if (collectors.size() != 0) {
					for (int i = 0; i < collectors.size(); i++) {
						Collector collector = collectors.get(i);
						filecollectors.add(dofilecollector(collector, languagecookie));
					}
				}
			}
			return new JsonResult<List<FileCollector>>(33, "展示采集器所有列表", filecollectors);
		} else {
			List<FileCollector> filecollectors = new ArrayList<FileCollector>();
			if (opeorgcookie.equals("1")) {
				List<Collector> collectors = collectordao.findByConditions(collectorcode, collectorassetcode,
						collectortype, collectorprotocol, collectormade, collectorstatus, null, opetypecookie);
				for (Collector collector : collectors) {
					filecollectors.add(dofilecollector(collector, languagecookie));
				}
			} else if (orgdao.findByOrgvalue(opeorgcookie).getOrgfather().equals("0")) {
				List<Org> orgs = orgdao.findByOrgfather(opeorgcookie);
				for (Org org : orgs) {
					List<Collector> collectors = collectordao.findByConditions(collectorcode, collectorassetcode,
							collectortype, collectorprotocol, collectormade, collectorstatus,
							Integer.parseInt(org.getOrgvalue()), opetypecookie);

					for (Collector collector : collectors) {
						filecollectors.add(dofilecollector(collector, languagecookie));
					}
				}
			} else {
				List<Collector> collectors = collectordao.findByConditions(collectorcode, collectorassetcode,
						collectortype, collectorprotocol, collectormade, collectorstatus, Integer.valueOf(opeorgcookie),
						opetypecookie);
				// 逐条查询
				if (collectors.size() != 0) {
					for (int i = 0; i < collectors.size(); i++) {
						Collector collector = collectors.get(i);
						filecollectors.add(dofilecollector(collector, languagecookie));
					}
				}
			}
			List<Collector> collectors = collectordao.findByConditions(collectorcode, collectorassetcode, collectortype,
					collectorprotocol, collectormade, collectorstatus, Integer.valueOf(opeorgcookie), opetypecookie);

			// 逐条查询
			if (collectors.size() != 0) {
				for (int i = 0; i < collectors.size(); i++) {

					Collector collector = collectors.get(i);

					filecollectors.add(dofilecollector(collector, languagecookie));
				}
			}

			return new JsonResult<List<FileCollector>>(40, "展示采集器条件查询列表", filecollectors);

		}

	}

	// 查询字段列表

	// 增加
	@Transactional
	public JsonResult<Collector> addcollector(String collectorcode, String collectorassetcode, String collectortype,
			String collectoraddress,String collectorprotocol, String collectormade, String opeorgcookie, String opetypecookie) {

		if (Utils.notNullAndEmpty(collectorcode) && Utils.notNullAndEmpty(collectorassetcode)
				&& Utils.notNullAndEmpty(collectortype) && Utils.notNullAndEmpty(collectorprotocol)
				&& Utils.notNullAndEmpty(collectormade) && Integer.valueOf(opeorgcookie) != null
				&& Utils.notNullAndEmpty(opetypecookie) && Utils.notNullAndEmpty(collectoraddress)) {

			// 判断字段，输入不能与已有的重复
			if (collectordao.findByCollectorcodeAndCollectorstatusNot(collectorcode, "4") == null) {
				// 判断字段，输入不能与已有的重复
				if (collectordao.findByCollectorassetcodeAndCollectorstatusNot(collectorassetcode, "4") == null) {

					Collector collector = new Collector();
					collector.setCollectorcode(collectorcode);
					collector.setCollectorassetcode(collectorassetcode);
					collector.setCollectortype(collectortype);
					collector.setCollectorprotocol(collectorprotocol);
					collector.setCollectoraddress(collectoraddress);
					collector.setCollectormade(collectormade);
					collector.setCollectororg(Integer.valueOf(opeorgcookie));
					collector.setCollectorstatus("1");
					collector.setType(opetypecookie);
					collectordao.save(collector);

					log.info("AddCollectorSuccess");

					return new JsonResult<Collector>(31, "增加采集器成功", collector);
				}
				log.info("AddCollectorFail..ErrorCollectorassetcode");
				return new JsonResult<Collector>(38, "增加采集器失败..资产编码不能重复", null);
			}
			log.info("AddCollectorFail..ErrorCollectorcode");
			return new JsonResult<Collector>(41, "增加采集器失败..出厂编码不能重复", null);

		}
		log.info("AddCollectorFail..Null");
		return new JsonResult<Collector>(32, "增加采集器失败..数值不能为空", null);

	}

	// 删除
	@Transactional
	public JsonResult<Collector> deletecollector(Integer collectorid) {
		if (collectorid != null) {
			if (collectordao.findOne(collectorid).getCollectorstatus().equals("1")) {

				collectordao.updateCollector("4", collectorid);

				return new JsonResult<Collector>(34, "删除采集器成功", null);
			}
			return new JsonResult<Collector>(34, "删除采集器失败..该采集器正在与采集器连接", null);
		}
		return new JsonResult<Collector>(44, "删除采集器失败..ID为空", null);
	}

	// 强制删除
	@Transactional
	public JsonResult<Collector> forcedeletecollector(Integer collectorid) {
		if (collectorid != null) {
			// 改采集器状态
			collectordao.updateCollector("4", collectorid);
			// 改变有关采集点的表状态为未运行
			for (int i = 0; i < measuringpointdao.findByCollectorid(collectorid).size(); i++) {

				Integer meterid = measuringpointdao.findByCollectorid(collectorid).get(i).getMeterid();
				meterdao.updateMeter("2", meterid);
			}
			// 如果删除改采集器后采集终端下没有设备，更改采集终端状态
			Integer size1 = measuringpointdao.findByCollectorid(collectorid).size();
			Integer size2 = measuringpointdao
					.findByTerminalid(measuringpointdao.findByCollectorid(collectorid).get(0).getTerminalid()).size();
			if (size1 == size2) {
				terminaldao.updateTerminal("1",
						measuringpointdao.findByCollectorid(collectorid).get(0).getTerminalid());
			}
			// 删除有关采集点信息
			if (!measuringpointdao.findByCollectorid(collectorid).isEmpty()) {
				measuringpointdao.deleteByCollectorid(collectorid);
			}
			return new JsonResult<Collector>(34, "删除采集器成功", null);
		}
		return new JsonResult<Collector>(44, "删除采集器失败..ID为空", null);
	}

	// 修改页面
	@Transactional
	public JsonResult<FileCollector> updateviewcollector(Integer collectorid, String languagecookie) {
		if (collectorid != null) {
			Collector collector = collectordao.findOne(collectorid);
			log.info(collectorid.toString());
			log.info("updateviewSuccess");

			return new JsonResult<FileCollector>(35, "查单个采集器", dofilecollector(collector, languagecookie));
		}
		return new JsonResult<FileCollector>(43, "查单个采集器失败..ID为空", null);
	}

	// 修改
	@Transactional
	public JsonResult<Collector> updatecollector(Integer collectorid, String collectorcode, String collectorassetcode,
			String collectortype, String collectoraddress,String collectorprotocol, String collectormade, String opeorgcookie,
			String collectorstatus, String opetypecookie) {

		Collector collector = new Collector();

		if (Utils.isEmpty(collectorcode) || Utils.isEmpty(collectorassetcode) || Utils.isEmpty(collectortype)
				|| Utils.isEmpty(collectorprotocol) || Utils.isEmpty(collectormade) || Utils.isEmpty(collectorstatus)
				|| collectorid == null || Utils.isNull(opeorgcookie) || Utils.isNull(opetypecookie)
				|| Utils.isEmpty(collectoraddress)) {

			return new JsonResult<Collector>(37, "修改采集器失败..数值不能为空", null);
		} else {

			collector.setCollectorid(collectorid);
			collector.setCollectororg(Integer.valueOf(opeorgcookie));
			collector.setType(opetypecookie);

			// 判断collectorcode
			if (Utils.isNull(collectorcode)) {

				collector.setCollectorcode(collectordao.findOne(collectorid).getCollectorcode());
			} else if (collectordao.findByCollectorcodeAndCollectorstatusNot(collectorcode, "4") != null
					&& !(collectordao.findOne(collectorid).getCollectorcode().equals(collectorcode))) {

				return new JsonResult<Collector>(42, "修改采集器失败..出厂编码不能重复", null);

			} else {

				collector.setCollectorcode(collectorcode);

			}
			// 判断collectorassetcode
			if (Utils.isNull(collectorassetcode)) {

				collector.setCollectorassetcode(collectordao.findOne(collectorid).getCollectorassetcode());
			} else if (collectordao.findByCollectorassetcodeAndCollectorstatusNot(collectorassetcode, "4") != null
					&& !(collectordao.findOne(collectorid).getCollectorassetcode().equals(collectorassetcode))) {

				return new JsonResult<Collector>(39, "修改采集器失败..资产编码不能重复", null);

			} else {

				collector.setCollectorassetcode(collectorassetcode);
			}
			// 判断collectortype
			if (Utils.isNull(collectortype)) {

				collector.setCollectortype(collectordao.findOne(collectorid).getCollectortype());
			} else {

				collector.setCollectortype(collectortype);
			}
			// 判断collectorprotocol
			if (Utils.isNull(collectorprotocol)) {

				collector.setCollectorprotocol(collectordao.findOne(collectorid).getCollectorprotocol());
			} else {
				collector.setCollectorprotocol(collectorprotocol);
			}
			
			// 判断collectoraddress
			if (Utils.isNull(collectoraddress)) {

				collector.setCollectoraddress(collectordao.findOne(collectorid).getCollectoraddress());
			} else {
				collector.setCollectoraddress(collectoraddress);
			}
			// 判断collectormade
			if (Utils.isNull(collectormade)) {

				collector.setCollectormade(collectordao.findOne(collectorid).getCollectormade());
			} else {
				collector.setCollectormade(collectormade);
			}
			// 判断collectorstatus
			if (Utils.isNull(collectorstatus)) {

				collector.setCollectorstatus(collectordao.findOne(collectorid).getCollectorstatus());
			} else {
				collector.setCollectorstatus(collectorstatus);
			}

			collectordao.save(collector);
			log.info("UpdateSuccess");
			return new JsonResult<Collector>(36, "修改采集器成功", collector);

		}

	}

	// 组装前端类
	public FileCollector dofilecollector(Collector collector, String languagecookie) {

		FileCollector filecollector = new FileCollector();

		// 添加基本信息

		filecollector.setCollectorid(collector.getCollectorid());
		filecollector.setCollectorcode(collector.getCollectorcode());
		filecollector.setCollectorassetcode(collector.getCollectorassetcode());
		filecollector.setCollectortype(collector.getCollectortype());
		filecollector.setCollectortypename(sysdicservice.finddisplay(sysdicservice.gettype(collector.getType(), "2"),
				collector.getCollectortype(), languagecookie));// 0004
		filecollector.setCollectorprotocol(collector.getCollectorprotocol());
		filecollector.setCollectorprotocolname(
				sysdicservice.finddisplay("0010", collector.getCollectorprotocol(), languagecookie));
		filecollector.setCollectormade(collector.getCollectormade());
		filecollector
				.setCollectormadename(sysdicservice.finddisplay("0008", collector.getCollectormade(), languagecookie));
		filecollector.setCollectororg(collector.getCollectororg());
		filecollector.setCollectororgname(
				sysdicservice.finddisplay("0001", collector.getCollectororg().toString(), languagecookie));
		filecollector.setCollectorstatus(collector.getCollectorstatus());
		filecollector.setCollectorstatusname(
				sysdicservice.finddisplay("0009", collector.getCollectorstatus(), languagecookie));

		return filecollector;
	}

}
